<?php
namespace App\Controller;
use Think\Controller;
class UploadController extends Controller {
    public function upload(){
        $openid=$_POST['openid'];
        $base64=$_POST['base64'];
        if($openid && $base64){
            $user=M('user');
            $user_select_res=$user->where(array('user_openid'=>$openid))->select();
            if($user_select_res){
                $user_id=$user_select_res[0]['id'];        
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有该用户'));
                return ;        
            }
            $upload_res=base64_upload($base64,$user_id);
            if($upload_res){

                // $this->ajaxReturn(array(
                //     'return_code'=>1,
                //     'return_msg'=>'上传成功',
                //     'return_body'=>array('imgUrl'=>$upload_res)
                //     )
                // );
                

                $uploadimg=M('uploadimg');
                $add_data=array(
                    'user_id'=>$user_id,
                    'user_openid'=>$openid,
                    'img_url'=>$upload_res
                );
                $uploadimg_add_res=$uploadimg->data($add_data)->add();
                if($uploadimg_add_res){
                    $add_data['userInfo']=$user_select_res[0];
                    // 推送消息
                    push_message('check_user_uploadimg',$add_data);
    
                    $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'上传成功','return_body'=>array()));                    
                }else{
                    $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'上传失败'));                    
                }
                
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'上传失败'));                
            }


        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }
        
    }


    public function savepath(){
        $openid=$_POST['openid'];
        $imgurl=$_POST['imgUrl'];

        if($openid && $imgurl){
            // 通过openid 获取用户id
            $user=M('user');
            $user_select_res=$user->where(array('user_openid'=>$openid))->select();
            if($user_select_res){
                $user_id=$user_select_res[0]['id'];        
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有该用户'));
                return ;        
            }

            $uploadimg=M('uploadimg');
            $add_data=array(
                'user_id'=>$user_id,
                'user_openid'=>$openid,
                'img_url'=>$imgurl
            );
            
            $uploadimg_add_res=$uploadimg->data($add_data)->add();
            if($uploadimg_add_res){
                $add_data['userInfo']=$user_select_res[0];
                // 推送消息
                push_message('check_user_uploadimg',$add_data);

                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'上传成功','return_body'=>array()));                    
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'上传失败'));                    
            }

        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));            
        }


    }


}
